package com.kpf.springcloud.exception;

import com.kpf.springcloud.common.ResponseEnum;
import com.kpf.springcloud.common.ResultJson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常信息捕获, 全局配置
 */
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    protected ResultJson handleException(Exception ex, HttpServletRequest req) {
        // 基础异常返回信息
        if (ex instanceof BusinessException) {
            BusinessException businessException = (BusinessException) ex;
            return ResultJson.failed(businessException.getCode(), businessException.getMsg());
        }
        //返回信息
        if (ex instanceof BaseException) {
            BaseException invokeException = (BaseException) ex;
            return ResultJson.failed(invokeException.getCode(), invokeException.getMsg());
        }
        return ResultJson.failed(ResponseEnum.FAIL);
    }

}
